//package com.zdp.distributedemo.service;
//
//
//import com.zdp.distributedemo.mapper.OrderItemMapper;
//import com.zdp.distributedemo.mapper.OrderMapper;
//import com.zdp.distributedemo.mapper.ProductMapper;
//import com.zdp.distributedemo.model.Order;
//import com.zdp.distributedemo.model.OrderItem;
//import com.zdp.distributedemo.model.Product;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.transaction.TransactionDefinition;
//import org.springframework.transaction.TransactionStatus;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.util.Date;
//import java.util.concurrent.locks.Lock;
//import java.util.concurrent.locks.ReentrantLock;
//
///**
// * @author sesshomaru
// * @date 2020/10/9 19:40
// */
//@Service
//@Slf4j
//public class OrderService {
//
//    @Resource
//    private OrderMapper orderMapper;
//    @Resource
//    private OrderItemMapper orderItemMapper;
//    @Resource
//    private ProductMapper productMapper;
//    //购买商品id
//    private int purchaseProductId = 100100;
//    //购买商品数量
//    private int purchaseProductNum = 1;
//    // 手动控制事务
//    @Autowired
//    private PlatformTransactionManager platformTransactionManager;
//    @Autowired
//    private TransactionDefinition transactionDefinition;
//
//
//    public synchronized Integer createOrder() throws Exception {
//        Product product = null;
//
//        // 进入方法是首先获取事务
//        TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
//
//        product = productMapper.selectByPrimaryKey(purchaseProductId);
//        if (product == null) {
//            // 异常时事务回滚
//            platformTransactionManager.rollback(transactionStatus);
//            throw new Exception("购买商品：" + purchaseProductId + "不存在");
//        }
//        //商品当前库存
//        Integer currentCount = product.getCount();
//        log.info("当前线程:{},库存数:{}", Thread.currentThread().getName(), currentCount);
//
//        //校验库存
//        if (purchaseProductNum > currentCount) {
//            // 异常时事务回滚
//            platformTransactionManager.rollback(transactionStatus);
//            throw new Exception("商品" + purchaseProductId + "仅剩" + currentCount + "件，无法购买");
//        }
//        productMapper.updateProductCount(purchaseProductNum, new Date(), product.getId());
//
//        // 开启就一定要提交
//        platformTransactionManager.commit(transactionStatus);
//
//
//        // 再次获取事务
//        Order order = new Order();
//        order.setOrderAmount(product.getPrice().multiply(new BigDecimal(purchaseProductNum)));
//        order.setOrderStatus(1);//待处理
//        order.setReceiverName("xxx");
//        order.setReceiverMobile("13311112222");
//        order.setCreateTime(new Date());
//        order.setCreateUser("xxx");
//        order.setUpdateTime(new Date());
//        order.setUpdateUser("xxx");
//        orderMapper.insertSelective(order);
//
//        OrderItem orderItem = new OrderItem();
//        orderItem.setOrderId(order.getId());
//        orderItem.setProductId(product.getId());
//        orderItem.setPurchasePrice(product.getPrice());
//        orderItem.setPurchaseNum(purchaseProductNum);
//        orderItem.setCreateTime(new Date());
//        orderItem.setUpdateTime(new Date());
//        orderItemMapper.insertSelective(orderItem);
//
//        // 方法最后手动的提交事务
//        platformTransactionManager.commit(transactionStatus);
//
//        return order.getId();
//    }
//
//}
//
//
